// Copyright (c), 2006, adopus consulting GmbH Switzerland, all rights reserved.
package com.purej.ui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * A StatusBar panel that shows messages from background tasks and a progress bar.
 * See the {@link Task} class for details.
 *
 * @author Stefan Mueller
 */
public class StatusBar implements TaskListener {
  private Application _application;
  private final JLabel _messageLabel;
  private final JProgressBar _progressBar;
  private final JLabel _interruptLabel;
  private JPanel _statusBarPanel;

  /**
   * Constructs a panel that displays messages/progress/state properties of the {@code taskMonitor's} foreground task.
   *
   * @param application the application callback
   */
  public StatusBar(Application application) {
    super();
    _application = application;
    _messageLabel = new JLabel("");
    _progressBar = new JProgressBar(0, 100);
    _progressBar.setEnabled(false);
    _progressBar.setBorderPainted(false);
    _interruptLabel = new JLabel(_application.getIcon("icons/interrupt-grey.png"));

    // Create UI & add listener:
    createUI();
    addListeners();
  }

  private void createUI() {
    FormLayout layout = new FormLayout(// JGoodies
        "2dlu, pref:grow, 4dlu, 100dlu, 1dlu, pref", // 5 columns
        "pref, 6dlu, pref" // 3 rows
    );
    PanelBuilder builder = new PanelBuilder(layout);
    builder.border(Borders.DIALOG);
    CellConstraints cc = new CellConstraints();

    builder.addSeparator("", cc.xyw(1, 1, 6));
    builder.add(_messageLabel, cc.xy(2, 3));
    builder.add(_progressBar, cc.xy(4, 3));
    builder.add(_interruptLabel, cc.xy(6, 3));
    _statusBarPanel = builder.getPanel();
  }

  private void addListeners() {
    // Add this status bar as task listener:
    _application.addTaskListener(this);

    // Add the mouse listener to the interrupt label:
    _interruptLabel.addMouseListener(new MouseListener() {

      /**
       * {@inheritDoc}
       */
      @Override
      public void mouseClicked(MouseEvent inEvent) {
        Task<?> task = _application.getCurrentTask();
        if (task != null && task.isInterruptable()) {
          int result = JOptionPane.showConfirmDialog(_application, "Do you really want to interrupt the currently executing task?", "Interrupt Task",
              JOptionPane.OK_CANCEL_OPTION);
          if (result == JOptionPane.OK_OPTION) {
            task.interrupt();
          }
        }
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public void mouseEntered(MouseEvent event) {
        setIconShadow();
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public void mouseExited(MouseEvent event) {
        setIconPlain();
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public void mousePressed(MouseEvent event) {
        setIconPlain();
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public void mouseReleased(MouseEvent event) {
        setIconShadow();
      }
    });
  }

  /**
   * @return the status bar panel
   */
  public JPanel getStatusBarPanel() {
    return _statusBarPanel;
  }

  /**
   * Sets the status message.
   *
   * @param message the message to be set
   */
  public void setStatusMessage(String message) {
    _messageLabel.setText(message == null ? "" : message);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void taskStarted(Task<?> task) {
    setIconPlain();
    _progressBar.setEnabled(true);
    _progressBar.setIndeterminate(true);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void taskFinished(Task<?> task, String message) {
    setStatusMessage(message);
    setIconGrey();
    _progressBar.setIndeterminate(false);
    _progressBar.setEnabled(false);
    _progressBar.setValue(0);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void taskStatusMessage(Task<?> task, String message) {
    setStatusMessage(message);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void taskErrorMessage(Task<?> task, String message) {
    // Ignored...
  }

  private void setIconGrey() {
    _interruptLabel.setIcon(_application.getIcon("icons/interrupt-grey.png"));
  }

  private void setIconPlain() {
    Task<?> task = _application.getCurrentTask();
    if (task != null && task.isInterruptable()) {
      _interruptLabel.setIcon(_application.getIcon("icons/interrupt-plain.png"));
    }
  }

  private void setIconShadow() {
    Task<?> task = _application.getCurrentTask();
    if (task != null && task.isInterruptable()) {
      _interruptLabel.setIcon(_application.getIcon("icons/interrupt-shadow.png"));
    }
  }
}
